Allow manual input for boolean and array form-configurable types

Dominik Sander 10 年之前
父节点
当前提交
d41439e3be

+ 13 - 5
app/assets/javascripts/components/form_configurable.js.coffee

@@ -10,7 +10,7 @@ $ ->
10 10
     returnedResults = {}
11 11
     completableDefaultOptions = (input) ->
12 12
       results: [
13
-        (returnedResults[$(input).data('attribute')] || {text: 'Options', children: [{id: '', text: 'loading ...'}]}),
13
+        (returnedResults[$(input).data('attribute')] || {text: 'Options', children: [{id: undefined, text: 'loading ...'}]}),
14 14
         {
15 15
           text: 'Current',
16 16
           children: [id: $(input).val(), text: $(input).val()]
@@ -21,7 +21,7 @@ $ ->
21 21
         },
22 22
       ]
23 23
 
24
-    $("input[role=validatable], select[role=validatable]").on 'change', (e) =>
24
+    $("input[role~=validatable], select[role~=validatable]").on 'change', (e) =>
25 25
       form_data = getFormData(e.currentTarget)
26 26
       form_group = $(e.currentTarget).closest('.form-group')
27 27
       $.ajax '/agents/validate',
@@ -38,7 +38,7 @@ $ ->
38 38
           form_group.find('.glyphicon-remove').removeClass('hidden')
39 39
           returnedResults = {}
40 40
 
41
-    $("input[role=validatable], select[role=validatable]").trigger('change')
41
+    $("input[role~=validatable], select[role~=validatable]").trigger('change')
42 42
 
43 43
     $.each $("input[role~=completable]"), (i, input) ->
44 44
       $(input).select2(
@@ -63,6 +63,14 @@ $ ->
63 63
         type: 'POST',
64 64
         data: form_data
65 65
         success: (data) ->
66
-          updateDropdownData(form_data, e.currentTarget, $.map(data, (d) -> {id: d.value, text: d.name}))
66
+          updateDropdownData(form_data, e.currentTarget, data)
67 67
         error: (data) ->
68
-          updateDropdownData(form_data, e.currentTarget, [{id: undefined, text: 'Error loading data.'}])
68
+          updateDropdownData(form_data, e.currentTarget, [{id: undefined, text: 'Error loading data.'}])
69
+
70
+    $("input[type=radio][role~=form-configurable]").change (e) ->
71
+      input = $(e.currentTarget).parents().siblings("input[data-attribute=#{$(e.currentTarget).data('attribute')}]")
72
+      if $(e.currentTarget).val() == 'manual'
73
+        input.removeClass('hidden')
74
+      else
75
+        input.val($(e.currentTarget).val())
76
+        input.addClass('hidden')

+ 9 - 0
app/concerns/form_configurable.rb

@@ -43,6 +43,15 @@ module FormConfigurable
43 43
         options[:roles] = [options[:roles]]
44 44
       end
45 45
 
46
+      if options[:type] == :array
47
+        options[:roles] << :completable
48
+        class_eval <<-EOF
49
+          def complete_#{name}
50
+            #{options[:values]}.map { |v| {text: v, id: v} }
51
+          end
52
+        EOF
53
+      end
54
+
46 55
       _form_configurable_fields[name] = options
47 56
     end
48 57
 

+ 1 - 1
app/models/agents/basecamp_agent.rb

@@ -50,7 +50,7 @@ module Agents
50 50
     def complete_project_id
51 51
       service.prepare_request
52 52
       response = HTTParty.get projects_url, request_options.merge(query_parameters)
53
-      response.map { |p| {name: "#{p['name']} (#{p['id']})", value: p['id']}}
53
+      response.map { |p| {text: "#{p['name']} (#{p['id']})", id: p['id']}}
54 54
     end
55 55
 
56 56
     def validate_options

+ 1 - 1
app/models/agents/hipchat_agent.rb

@@ -53,7 +53,7 @@ module Agents
53 53
     end
54 54
 
55 55
     def complete_room_name
56
-      client.rooms.collect { |room| {name: room.name, value: room.name} }
56
+      client.rooms.collect { |room| {text: room.name, id: room.name} }
57 57
     end
58 58
 
59 59
     def validate_options

+ 11 - 8
app/presenters/form_configurable_agent_presenter.rb

@@ -16,7 +16,7 @@ class FormConfigurableAgentPresenter < Decorator
16 16
   def option_field_for(attribute)
17 17
     data = @agent.form_configurable_fields[attribute]
18 18
     value = @agent.options[attribute.to_s] || @agent.default_options[attribute.to_s]
19
-    html_options = {role: data[:roles].join(' '), data: {attribute: attribute}}
19
+    html_options = {role: (data[:roles] + ['form-configurable']).join(' '), data: {attribute: attribute}}
20 20
 
21 21
     case data[:type]
22 22
     when :text
@@ -24,17 +24,20 @@ class FormConfigurableAgentPresenter < Decorator
24 24
     when :boolean
25 25
       @view.content_tag 'div' do
26 26
         @view.concat(@view.content_tag('label', class: 'radio-inline') do
27
-          @view.concat @view.radio_button_tag "agent[options][#{attribute}]", 'true', @agent.send(:boolify, value), html_options
28
-          @view.concat "Yes"
27
+          @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'true', @agent.send(:boolify, value) == true, html_options
28
+          @view.concat "True"
29 29
         end)
30 30
         @view.concat(@view.content_tag('label', class: 'radio-inline') do
31
-          @view.concat @view.radio_button_tag "agent[options][#{attribute}]", 'false', !@agent.send(:boolify, value), html_options
32
-          @view.concat "No"
31
+          @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'false', @agent.send(:boolify, value) == false, html_options
32
+          @view.concat "False"
33 33
         end)
34
+        @view.concat(@view.content_tag('label', class: 'radio-inline') do
35
+          @view.concat @view.radio_button_tag "agent[options][#{attribute}_radio]", 'manual', @agent.send(:boolify, value) == nil, html_options
36
+          @view.concat "Manual Input"
37
+        end)
38
+        @view.concat(@view.text_field_tag "agent[options][#{attribute}]", value, html_options.merge(:class => "form-control #{@agent.send(:boolify, value) != nil ? 'hidden' : ''}"))
34 39
       end
35
-    when :array
36
-      @view.select_tag("agent[options][#{attribute}]", @view.options_for_select(data[:values], value), html_options.merge(class: "form-control"))
37
-    when :string
40
+    when :array, :string
38 41
       @view.text_field_tag "agent[options][#{attribute}]", value, html_options.merge(:class => 'form-control')
39 42
     end
40 43
   end